﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Mobile.GandaInvitaciones.Base;

namespace Mobile.GandaInvitaciones
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        #region Propiedades
        public string Titulo
        {
            get { return lblTitulo.Text; }
            set { lblTitulo.Text = value; }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            bool esMobile = false;
            string strUserAgent = Request.UserAgent.ToString().ToLower();

            if(!Page.IsPostBack)
            {
                if (strUserAgent != null)
                {
                    if (Request.Browser.IsMobileDevice == true || strUserAgent.Contains("iphone") ||
                    strUserAgent.Contains("blackberry") || strUserAgent.Contains("mobile") ||
                    strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") ||
                    strUserAgent.Contains("palm"))
                    {
                        esMobile = true;
                    }
                }

                if (Session["AplicaVisita"] != null)
                {
                    try
                    {
                        ////string SourceXML = @"http://api.hostip.info/?ip=" + PageBase.GetDireccionIp(Request);
                        string SourceXML = @"http://api.ipinfodb.com/v3/ip-city/?key=f05d985c185ef43779b78354a2e4bc03c9fa8104c04a457f7e91028f56efe0e5&ip=" + PageBase.GetDireccionIp(Request) + "&format=xml";
                        DataSet ds = new DataSet();
                        ds.ReadXml(SourceXML);
                        //DataTable dt = ds.Tables["Hostip"];
                        DataTable dt = ds.Tables["Response"];

                        //string log;
                        //log = PageBase.GetDireccionIp(Request) + "\n\r" + DateTime.Now + "\n\r" +
                        //      strUserAgent + "\n\r" +
                        //      Request.Browser.Platform + "\n\r" +
                        //      PageBase.GetReferencesUrl(Request) + "\n\r";

                        //if (dt.Rows.Count > 0)
                        //{
                        //    log = log + dt.Rows[0]["cityName"].ToString() + "\n\r" +
                        //          dt.Rows[0]["regionName"].ToString() + "\n\r" +
                        //          dt.Rows[0]["countryName"].ToString() + "\n\r" +
                        //          dt.Rows[0]["latitude"].ToString() + "\n\r" +
                        //          dt.Rows[0]["longitude"].ToString();
                        //}

                        //GandA.Component.Log.Log.WriteLog(log, "Visita");

                        CommonServiceReference.Visita obj = new CommonServiceReference.Visita();
                        obj.IpVista = PageBase.GetDireccionIp(Request);
                        obj.FechaVisita = DateTime.Now;
                        obj.Explorador = strUserAgent;
                        obj.SistemaOperativo = Request.Browser.Platform;
                        obj.ReferenciaUrl = PageBase.GetReferencesUrl(Request);
                        obj.EsMobile = esMobile;
                        if (dt.Rows.Count > 0)
                        {
                            obj.Ciudad = dt.Rows[0]["cityName"].ToString();
                            obj.Estado = dt.Rows[0]["regionName"].ToString();
                            obj.Pais = dt.Rows[0]["countryName"].ToString();
                            obj.Latitud = dt.Rows[0]["latitude"].ToString();
                            obj.Longitud = dt.Rows[0]["longitude"].ToString();
                        }

                        CommonServiceReference.CommonServiceClient srv = new CommonServiceReference.CommonServiceClient();
                        srv.GuardaVisita(obj);
                    }
                    catch (Exception ex)
                    {
                        GandA.Component.Log.Log.WriteLog(ex, "Error en logue de Visita");
                    }
                    finally
                    {
                        Session.Remove("AplicaVisita");
                    }
                }
            }
        }

        public void alert(string msg)
        {
            if (Page.IsPostBack)
            {
                ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "alert", "alert('" + msg + "');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "alert", "alert('" + msg + "');", true);
            }
        }

        public void alert(string msg, string codigoExtra)
        {
            if (Page.IsPostBack)
            {
                ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "alert", "alert('" + msg + "');" + codigoExtra, true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "alert", "alert('" + msg + "');" + codigoExtra, true);
            }
        }

        public void alertExito()
        {
            alert("Operación exitosa");
        }

        public void alertExito(string codigoExtra)
        {
            alert("Operación exitosa", codigoExtra);
        }

        public void alertSinDatos()
        {
            alert("No se encontraron resultados");
        }

        public void alertSinDatos(string codigoExtra)
        {
            alert("No se encontraron resultados", codigoExtra);
        }
    }
}
